package com.rzjm.exercise.module.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.rzjm.exercise.common.base.controller.BaseController;
import com.rzjm.exercise.common.base.data.DataTableLayUI;
import com.rzjm.exercise.common.log.annotation.Log;
import com.rzjm.exercise.common.utils.DateUtils;
import com.rzjm.exercise.common.utils.R;
import com.rzjm.exercise.common.utils.StringUtil;
import com.rzjm.exercise.common.utils.StringUtils;
import com.rzjm.exercise.module.domain.*;
import com.rzjm.exercise.module.service.*;
import com.rzjm.exercise.sys.system.user.domain.UserDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 问卷列表
 *
 * @author mr.zhou
 * @email zhouwei_nice@163.com
 * @date 2021-11-5 12:36:23
 */
@Controller
@RequestMapping("/module/apply/shop/device")
public class ApplyController extends BaseController {
    @Resource
    private ApplyShopDeviceRecordService applyShopDeviceRecordService;
    @Resource
    private GroupsService groupsService;
    @Resource
    private DeviceNodeService deviceNodeService;
    @Resource
    private DeviceService deviceService;

    @Log("查询页面")
    @GetMapping()
//    @RequiresPermissions("module:applyShopDeviceRecord:applyShopDeviceRecord")
    String temperatureLimit(Model model) {
        List<Groups> groups = groupsService.list();
        model.addAttribute("groups", groups);
        UserDO userDO = getUser();
        if(userDO.getUserType() > 1) {//集团用户
//            qw.eq("jituan_id", userDO.getGroupId());
            model.addAttribute("groupId", userDO.getGroupId());
        }
        return "module/applydevice/index";
    }

    @Log("查询饲料列表数据")
    @ResponseBody
    @GetMapping("/list")
//    @RequiresPermissions("module:applyShopDeviceRecord:applyShopDeviceRecord")
    public DataTableLayUI<ApplyShopDeviceRecord> list(DataTableLayUI<ApplyShopDeviceRecord> condition) {
        UserDO sysUser = getUser();
        Map<String, Object> map = condition.getSearchParams();
        String req_name = map.containsKey("search_like_req_name") ? map.get("search_like_req_name").toString() : null;
        QueryWrapper<ApplyShopDeviceRecord> qw = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(req_name)){
            qw.like("req_name",req_name);
        }
        if (sysUser.getUserType() < 2){

        }else if (sysUser.getUserType() == 2){
            qw.eq("apply_shop_group_id", sysUser.getGroupId());
        }else if (sysUser.getUserType() == 3){
            qw.eq("apply_shop_pianqu_id", sysUser.getPianquId());
        }else {
            qw.eq("apply_shop_id", sysUser.getShopId());
        }
        condition.setQw(qw);
        DataTableLayUI<ApplyShopDeviceRecord> dt = applyShopDeviceRecordService.pageSearchLayUI(condition);
        List<ApplyShopDeviceRecord> list = dt.getData();
        list.stream().map(a -> {
            String deviceTypeName = "";
            if (a.getApplyDeviceType() != null){
                if (a.getApplyDeviceType().contains("2")){
                    deviceTypeName += ",油罐测温";
                }
                if (a.getApplyDeviceType().contains("1") ){
                    deviceTypeName += ",制粒机测温";
                }
                if (a.getApplyDeviceType().contains("3") ){
                    deviceTypeName += ",锅炉测温";
                }
                if (a.getApplyDeviceType().contains("4") ){
                    deviceTypeName += ",筒仓测量";
                }
                if (a.getApplyDeviceType().contains("5") ){
                    deviceTypeName += ",油罐测量";
                }
            }
            a.setApplyDeviceTypeStr(deviceTypeName.replaceFirst(",",""));
            if (a.getCreateTime() != null){
                a.setCreateTimeStr(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", a.getCreateTime()));
            }
            if (a.getEffectTime() != null){
                a.setEffectTimeStr(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", a.getEffectTime()));
            }
            if (a.getExamineTime() != null){
                a.setExamineTimeStr(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", a.getExamineTime()));
            }
            return a;
        }).collect(Collectors.toList());
        return dt;
    }


    /**
     * 删除
     */
    @Log("删除饲料")
    @PostMapping( "/remove")
    @ResponseBody
    @RequiresPermissions("module:feed:remove")
    public R remove( Integer id) {
        if (applyShopDeviceRecordService.removeById(id)) {
            return R.ok();
        }
        return R.error();
    }
}
